package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CompanyResult;
import com.itheima.common.Result;
import com.itheima.mapper.BsUserMapper;
import com.itheima.mapper.HmSubjectsMapper;
import com.itheima.mapper.TagsMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.hm.HmSubjects;
import com.itheima.pojo.hm.HmSubjectsTags;
import com.itheima.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/*
 *@Parm
 *@ClassName TagsServiceImpl
 *@Date 2022/1/10 10:23
 */
@Service
public class TagsServiceImpl implements TagsService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TagsMapper tagsMapper;
    @Override
    public CompanyResult add(HmSubjectsTags tags) {
        tags.setAddDate(LocalDateTime.now());
        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        tags.setState(1);
        tags.setCreatorID(userId);
        tags.setTotals(0);
        tagsMapper.insert(tags);
        CompanyResult companyResult = new CompanyResult();
        companyResult.setId(tags.getId());
        return companyResult;
    }

    @Autowired
    private BsUserMapper bsUserMapper;

    @Autowired
    private HmSubjectsMapper hmSubjectsMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result findPage(Integer page, Integer pagesize, String tagName, Integer state,Integer subjectID) {
        IPage<HmSubjectsTags> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<HmSubjectsTags> wrapper = new LambdaQueryWrapper<>();
        if(tagName != null){
            wrapper.like(HmSubjectsTags::getTagName,tagName);
        }
        if(state != null){
            wrapper.eq(HmSubjectsTags::getState,state);
        }
        if(subjectID != null){
            wrapper.eq(HmSubjectsTags::getSubjectID,subjectID);
        }
        Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
        BsUser bsUser = bsUserMapper.selectById(userId);
        IPage<HmSubjectsTags> hmSubjectsTagsIPage = tagsMapper.selectPage(p, wrapper);
        List<HmSubjectsTags> hmSubjectsTags = hmSubjectsTagsIPage.getRecords();
        for (HmSubjectsTags hmSubjectsTag : hmSubjectsTags) {
            hmSubjectsTag.setUsername(bsUser.getUsername());
            HmSubjects hmSubjects = hmSubjectsMapper.selectById(hmSubjectsTag.getSubjectID());
            hmSubjectsTag.setSubjectName(hmSubjects.getSubjectName());
        }
        Result result = new Result();
        result.setCounts(hmSubjectsTagsIPage.getTotal());
        result.setPagesize(pagesize);
        result.setPage(page);
        result.setItems(hmSubjectsTagsIPage.getRecords());
        result.setPages(hmSubjectsTagsIPage.getPages());
        return result;

    }

    @Override
    public CompanyResult updateById(HmSubjectsTags tags) {
        int i = tagsMapper.updateById(tags);
        if(i <= 0){
             return new CompanyResult(false);
        }
        return new CompanyResult(true);

    }

    @Override
    public CompanyResult deleteById(Integer id) {
        int i = tagsMapper.deleteById(id);
        if(i <= 0){
            return new CompanyResult(false);
        }
        return new CompanyResult(true);
    }

    @Override
    public CompanyResult updateByState(Integer id, Integer state) {
        HmSubjectsTags tags = new HmSubjectsTags();
        tags.setState(state);
        tags.setId(id);
        int i = tagsMapper.updateById(tags);
        if(i <= 0){
            return new CompanyResult(false);
        }
        return new CompanyResult(true);
    }

    @Override
    public HmSubjectsTags findOne(Integer id) {
        HmSubjectsTags hmSubjectsTags = tagsMapper.selectById(id);
        return hmSubjectsTags;
    }
}
